package com.cloudptt.api.product.func.sys;

import com.cloudptt.api.product.ISysOperator;
import com.cloudptt.api.product.func.IFunc;
import com.cloudptt.api.product.func.sys.fsm.SysAgent;
import com.cloudptt.api.product.func.sys.listener.SysListener;
import com.cloudptt.api.product.func.sys.listener.SysMsgListener;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.SysOperator;
import com.mcptt.config.DbOperatorManager;
import com.mcptt.config.local.SysConfig;
import com.mcptt.config.model.McIdInfo;
import com.mcptt.config.model.ServerInfo;

import android.content.Context;

public class SysFunc  implements IFunc
{
	public final static String Log_Key = "SysFunc";
	
//	private Context ctx = null;
	private SysOperator operator = null;
	private SysConfig config = null;
	private SysBroadcast broadcast = null;
	private SysAgent sysAgent = null;
	private DbOperatorManager dbManager = null;

	public SysFunc(Context ctx,SysConfig config,SysOperator operator,DbOperatorManager dbManager,IFunc superFunc,boolean networkDisconnect)
	{
		Log.d(Log_Key, "SysFunc networkDisconnect = " + networkDisconnect);
//		this.ctx = ctx;
		this.config = config;
		this.operator = operator;
		this.dbManager = dbManager;
		
		this.sysAgent = new SysAgent(ctx,this.config,this.operator,superFunc,networkDisconnect);
//		sysAgent.doSysAction(SysConst.SysAction_Auth);
		
		this.broadcast = new SysBroadcast(ctx,sysAgent);
	}

	public ISysOperator getSysOperator()
	{
		return sysAgent;
	}
//	public void setLoginListener(LoginListener loginListener) 
//	{
//		this.broadcast.setLoginListener(loginListener);
////		this.operator.setLoginListener(loginListener);
//	}
	
	public void setSysListener(SysListener sysListener) 
	{
		this.sysAgent.setSysListener(sysListener);
	}

	public void setSysMsgListener(SysMsgListener sysMsgListener) 
	{
		this.broadcast.setSysMsgListener(sysMsgListener);
	}
	
	
	public SysConfig getConfig() {
		return config;
	}
	public SysOperator getOperator() {
		return operator;
	}
	public SysStateInfo getSysStateInfo()
	{
		return sysAgent.getSysStateInfo();
	}
	
	@Override
	public void onLogined(McIdInfo mcIdInfo,ServerInfo info,boolean isLoginSuc) {
		// TODO Auto-generated method stub
	}
	@Override
	public void doNetworkChange(boolean isConn)
	{
		Log.d(Log_Key, "doNetworkChange isConn = " + isConn);
		this.sysAgent.setNetworkDisconnect(isConn);
	}
	@Override
	public void doForceDetach()
	{
		Log.d(Log_Key, "doForceDetach");
		this.broadcast.clearResourceForLogout();
		this.sysAgent.logout();
		this.operator.logout();
	}
	@Override
	public void doLogout()
	{
		Log.d(Log_Key, "logout");
		this.broadcast.clearResourceForLogout();
		this.sysAgent.logout();
		this.operator.logout();
		this.dbManager.getDbOperator().resetTables();
		this.config.clearUserInfoAfterLogout();
	}
	@Override
	public void doExit()
	{
		Log.d(Log_Key, "SysAgent exit");
		this.broadcast.clearResource();
		this.sysAgent.destory();
		this.operator.logout();

		this.broadcast = null;
		this.sysAgent = null;
		this.operator = null;
		this.config.clearUserInfoAfterExit();
		this.config  = null;
//		this.ctx = null;
	}

	@Override
	public void onLanChange(String lanType) {
		// TODO Auto-generated method stub
		
	}
//	public void setSysAddrListener(SysAddrListener sysAddrListener) {
//		if(sysAgent != null)
//		{
//			sysAgent.setSysAddrListener(sysAddrListener);
//		}
//	}

	@Override
	public void onKeepAlive(boolean isconnected) {
		// TODO Auto-generated method stub
		
	}
}
